const app = getApp();

Page({
  data: {
    done: false,
    cardName: "",
    priceList: [],
    maxConfigNumber: 6
  },

  getRechargeConfig() {
    app.ajax({
      url: "Partner/Recharge/getCard",
      data: {
        id: this.id
      },
      success: res => {
        wx.hideLoading()
        if (res.data.code == 1000) {
          let {
            card_name,
            package: packages
          } = res.data.data;
          let valuesPackages = Object.values(packages);
          let list = [];
          if (valuesPackages.length > 0) {
            for (let x in valuesPackages) {
              list.push({
                ...valuesPackages[x],
                time: new Date().getTime() + x
              });
            }
          } else {
            list = [{
              time: new Date().getTime(),
              card_money: '',
              give_money: ''
            }]
          }
          this.setData({
            done: true,
            cardName: card_name,
            priceList: list
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading()
        app.showToast("网络错误");
      }
    })
  },

  handleInput(e) {
    const {
      name,
      index
    } = e.currentTarget.dataset;
    let value = e.detail.value;
    this.setData({
      [`priceList[${index}].${name}`]: value
    })
  },

  submit(e) {
    let list = this.data.priceList;
    let nlist = {};
    for (let x in list) {
      if (list[x].card_money === '') {
        app.showToast(`第${parseInt(x) +1}项充值金额未填写`);
        break;
      } else if (list[x].give_money === '') {
        app.showToast(`第${parseInt(x)+1}项赠送金额未填写`);
        break;
      }
      Object.assign(nlist, {
        [`money_${x}`]: {
          card_money: list[x].card_money,
          give_money: list[x].give_money
        }
      })
    }
    wx.showLoading({
      mask: true
    })
    app.ajax({
      url: "Partner/Recharge/setPackage",
      data: {
        rechagre_card_id: this.id,
        package: nlist
      },
      success: res => {
        wx.hideLoading();
        if (res.data.code > 0) {
          app.showToast(res.data.message, "success");
          setTimeout(() => {
            wx.navigateBack({
              delta: 0,
            })
          }, 1500)
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        wx.hideLoading();
        app.showToast("网络错误");
      }
    })
  },



  onLoad: function (options) {
    this.id = options.id;
    wx.showLoading({})
    this.getRechargeConfig();
  },


  opreate(e) {
    const index = e.currentTarget.dataset.index;
    let list = this.data.priceList;
    if (list.length - 1 == index && list.length < this.data.maxConfigNumber) {
      list.push({
        time: new Date().getTime(),
        card_money: "",
        give_money: ""
      })
    } else { //reduce
      list.splice(index, 1);
    }
    this.setData({
      priceList: list
    })
  },
})